# import jieba
# import wordcloud
#
# import imageio
# mk = imageio.imread("E:\hnl\chinamap.png")
#
# w = wordcloud.WordCloud(width=1000,
#                         height=700,
#                         background_color='white',
#                         font_path='msyh.ttc',
#                         mask=mk,
#                         scale=15,
#                         stopwords={'曹操','孔明'})
#
# f = open('threekingdoms.txt',encoding='utf-8')
# txt = f.read()
# txtlist = jieba.lcut(txt)
# string = " ".join(txtlist)
#
# w.generate(string)
#
# w.to_ﬁle('output8-threekingdoms.png')
import jieba
import wordcloud
import imageio

# 1. 加载掩码图片（确保是黑白轮廓图）
mk = imageio.imread("E:\hnl\chinamap.png")
# mk = (mk > 128) * 255  # 如果需要转换彩色图为二值图

# 2. 配置词云参数
w = wordcloud.WordCloud(
    width=1000,
    height=700,
    background_color='pink',
    font_path='msyh.ttc',  # 或 'simhei.ttf'
    mask=mk,
    scale=5,  # 适当调低 scale 值
    stopwords={'曹操', '孔明'}
)

# 3. 读取并分词文本
with open('E:\hnl\金庸.txt', encoding='utf-8') as f:
    txt = f.read()
    txtlist = jieba.lcut(txt)  # 精确模式分词
    string = " ".join(txtlist)

# 4. 生成并保存词云
w.generate(string)
w.to_file('output8-threekingdoms.png')  # 注意文件名中的字母是普通 'f'